package com.es.system.controller;


import com.es.api.PageModel;
import com.es.api.SystemResult;
import com.es.system.domain.SysResource;
import com.es.system.service.SysResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import com.es.core.controller.BaseController;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 系统资源 前端控制器
 * </p>
 *
 * @author everysucceed
 * @since 2021-12-10
 */
@Controller
@RequestMapping("/system/resource")
public class SysResourceController extends BaseController {

    @Autowired
    private SysResourceService service;


    @GetMapping("/manage")
    public String manage() {
        return "system/resource/manage";
    }

    @GetMapping("/list")
    @ResponseBody
    public SystemResult<List<SysResource>> list(PageModel<SysResource> pageModel) {
        if (null == pageModel.getPage()) {
            pageModel.setPage(PageModel.DEFAULT_PAGE);
        }
        if (null == pageModel.getLimit()) {
            pageModel.setLimit(PageModel.DEFAULT_LIMIT);
        }
        SystemResult<List<SysResource>> result = service.page(pageModel.getPage(), pageModel.getLimit(), pageModel.getModel());

        return result;
    }

    @GetMapping("/add")
    public String addPage() {
        return "system/resource/add";
    }

    @PostMapping("/save")
    @ResponseBody
    public SystemResult<Serializable> save(@RequestBody SysResource sysResource) {
        return service.save(sysResource);
    }

    @DeleteMapping("/remove/{dataId}")
    @ResponseBody
    public SystemResult<String> remove(@PathVariable("dataId") String dataId) {
        boolean flag = service.removeById(Long.valueOf(dataId));
        if (flag) {
            return SystemResult.success("删除成功");
        } else {
            return SystemResult.fail("删除失败");
        }
    }
}
